public class MyQueue {
    static class ListNode{
        //数组域
        public int val;
        //两个指针域
        public ListNode prev;
        public ListNode next;
        //指定数值的构造方法
        public ListNode(int val){
            this.val=val;
        }
    }

    private ListNode front;
    private ListNode rear;

    //入队方法
    public int offer(int val){
        ListNode listNode=new ListNode(val);
        if(front==null){
            front=rear=listNode;
        }else{
            rear.next=listNode;
            listNode.prev=rear;
            rear=listNode;
        }
        return listNode.val;
    }

    //判空抛出异常方法
    private void checkPollisEmpty(){
        if(front==null){
            throw new PollEmptyException();
        }
    }

    //出队方法
    public int poll(){
        //出队判空抛出异常
        try{
            checkPollisEmpty();
        }catch (PollEmptyException e){
            e.printStackTrace();
        }
        int val= front.val;
        //判断是否为单一元素队列
        if(front.next==null)
        {
            front=rear=null;
        }
        front=front.next;
        front.prev=null;
        return val;

    }

    //获取队头元素方法
    public int peek(){
        //判空抛出异常
        try{
            checkPollisEmpty();
        }catch (PollEmptyException e){
            e.printStackTrace();
        }
        return front.val;
    }

    //判断队列是否为空
    public boolean isEmpty(){
        return front==null;
    }

}
